package com.hlhj.carview.aty

import android.content.Intent
import android.support.v4.view.ViewPager
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.CarInfoView
import com.hlhj.carview.R
import com.hlhj.carview.adapter.CarImgListAdp
import com.hlhj.carview.adapter.CarimgPagerAdp
import com.hlhj.carview.bean.CarInfoBean
import com.hlhj.carview.bean.ItemAskListBean
import com.hlhj.carview.http.Apis
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.CarInfoPresenter
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.aty_carinfo.*
import kotlinx.android.synthetic.main.lo_pubhead.*

/**
 * Created by Never Fear   on 2018\12\20 0020.
Never More....
 */
class CarInfoAty :BaseAty<CarInfoView,CarInfoPresenter>(), CarInfoView {
    private var carimgListAdp:CarImgListAdp?=null
    private var carimgPagerAdp:CarimgPagerAdp?=null

    private val listdatas=ArrayList<String>()
    private val pageDatas=ArrayList<CarInfoBean.Data>()

    override fun setCarinfo(carInfoBean: CarInfoBean) {

    }

    override fun initView() {
        tvTittle.text="车辆信息"
        btRightText.text="添加新车"

        carimgListAdp= CarImgListAdp(listdatas)
        carimgPagerAdp=CarimgPagerAdp(this,pageDatas)

        listView.adapter=carimgListAdp
        listView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)

        viewPager.adapter=carimgPagerAdp
    }
    override fun logicStart() {
        presenter?.getcarinfo(this)

        OkGo.post<CarInfoBean>(Urls.PubUrl)
                .headers("type","3")
                .params("uriPath","carInfo")
                .converter(object : MyJsonConvert<CarInfoBean>(){})
                .adapt(ObservableResponse<CarInfoBean>())
                .subscribeOn(Schedulers.io())
                .subscribe(object :CustomAction<Response<CarInfoBean>>(this){
                    override fun httpSuccess(t: Response<CarInfoBean>) {
                        val carInfoBean=t.body()
                        carInfoBean.data?.let { pageDatas.addAll(it) }
                        if (pageDatas.size>1)btNext.visibility=View.VISIBLE
                        carimgPagerAdp?.notifyDataSetChanged()
                        val info = carInfoBean.data?.get(0)
                        carNum.text="车牌号码：${info?.carNum}"
                        carType.text="车型：${info?.level}"
                        carimgListAdp?.notifyDataSetChanged()
                    }
                })

    }

    override fun setListener() {
        viewPager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position==0){
                    btLast.visibility=View.GONE
                }else{
                    btLast.visibility=View.VISIBLE

                }
                if (position==pageDatas.size-1){
                    btNext.visibility=View.GONE
                }else{
                    btNext.visibility=View.VISIBLE
                }
                val info = pageDatas[position]
                carNum.text="车牌号码：${info.carNum}"
                carType.text="车型：${info.level}"
                carimgListAdp?.notifyDataSetChanged()
            }
        })
        btLast.setOnClickListener {
            if (viewPager.currentItem!=0)
            viewPager?.currentItem=viewPager?.currentItem!!-1

        }

        btNext.setOnClickListener {
            viewPager?.currentItem=viewPager?.currentItem!!+1
        }
        btRightText.setOnClickListener {
            val intent = Intent(this, AddCarAty::class.java)
            startActivity(intent)
        }
    }

    override fun bindView(): CarInfoView {
        return this
    }

    override fun createPresenter(): CarInfoPresenter {
        return CarInfoPresenter()
    }

    override fun getContentID(): Int {
        return R.layout.aty_carinfo
    }
}